package com.xsy.de.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.xsy.de.entity.ResultMsg;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class SmsServiceImpl {
    private final static Logger log = LogManager.getLogger(SmsServiceImpl.class);

    @Value("${aliyun.sms.regionId}")
    private String REGIONID;

    @Value("${aliyun.sms.accessKeyId}")
    private String ACCESSKEYID;

    @Value("${aliyun.sms.secret}")
    private String SECRET;

    @Value("${aliyun.sms.domain}")
    private String DOMAIN;

    @Value("${aliyun.sms.version}")
    private String VERSION;

    @Value("${aliyun.sms.action}")
    private String ACTION;

    @Value("${aliyun.sms.templateCode}")
    private String TEMPLATECODE;

    @Value("${aliyun.sms.SignName}")
    private String SIGNNAME;

    public ResultMsg sndSmsCode(String phone, String code){
        log.debug("发送短信验证码-手机：" + phone + "-->" + code);
        ResultMsg resultMsg = new ResultMsg();
        resultMsg.setRtncode("1");
        try {
            log.debug("初始化短信参数...");
            DefaultProfile profile = DefaultProfile.getProfile(REGIONID, ACCESSKEYID, SECRET);
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest request = new CommonRequest();
            //request.setProtocol(ProtocolType.HTTPS);

            request.setMethod(MethodType.POST);
            request.setDomain(DOMAIN);
            request.setVersion(VERSION);
            request.setAction(ACTION);
            request.putQueryParameter("RegionId", REGIONID);
            request.putQueryParameter("PhoneNumbers", phone);
            request.putQueryParameter("SignName", SIGNNAME);
            request.putQueryParameter("TemplateCode", TEMPLATECODE);
            request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
            try {
                log.debug("执行短信服务请求...");
                CommonResponse response = client.getCommonResponse(request);
                log.debug("短信服务请求返回：" + response.getData());
                JSONObject result = JSONObject.parseObject(response.getData());
                if ("OK".equals(result.getString("Message"))){
                    resultMsg.setRtncode("0");
                    resultMsg.setRtninfo(result);
                    return resultMsg;
                }else {
                    resultMsg.setRtncode("4");
                    resultMsg.setRtninfo(result);
                    resultMsg.setErrmsg("错误码：" + result.getString("Code"));
                }
            } catch (ServerException e) {
                log.error("短信发送服务端异常：Code：" + e.getErrCode() + ";ErrMsg：" + e.getErrMsg() + ";Message：" + e.getMessage() );
                e.printStackTrace();
                resultMsg.setRtncode("4");
                resultMsg.setErrmsg(e.getMessage());
            } catch (ClientException e) {
                log.error("短信发送客户端异常："+e.getMessage());
                e.printStackTrace();
                resultMsg.setRtncode("3");
                resultMsg.setErrmsg(e.getMessage());
            }
        }catch (Exception e){
            log.error("短信发送异常："+e.getMessage());
            e.printStackTrace();
            resultMsg.setRtncode("2");
            resultMsg.setErrmsg(e.getMessage());
        }
        return resultMsg;
    }

}
